package com.acc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.acc.entity.EasyUIDataGridJSON;
import com.acc.entity.Msg;
import com.acc.entity.Standby;
import com.acc.entity.User;
import com.acc.service.AdminUserService;
import com.acc.service.RoleUriService;
import com.acc.service.UserStandbyService;
import com.acc.service.impl.AdminUserServiceImpl;
import com.acc.service.impl.RoleUriServiceImpl;
import com.acc.service.impl.UserStandbyServiceImpl;
import com.alibaba.fastjson.JSON;
/**
 * 
 * @author 朱春阳
 *
 */
public class AdminUserController extends InnerDispacherController2 {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 分页查询用户数据
	 * 
	 * @param request
	 * @param response
	 */
	public void pageUser(HttpServletRequest request, HttpServletResponse response) {
		AdminUserService adminUserService = new AdminUserServiceImpl();
		Integer page = null;
		Integer rows = null;
		// 获取分页数据
		try {
			page = Integer.parseInt(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}
		// 获取总数目
		Integer count = adminUserService.getAllUserSize();

		List<User> pageusers = adminUserService.getPageUsers(page, rows);

		EasyUIDataGridJSON easyUIDataGridJSON = new EasyUIDataGridJSON();
		easyUIDataGridJSON.setTotal(count);
		easyUIDataGridJSON.setRows(pageusers);

		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			// 转换成json格式
			response.setContentType("application/json");
			pw.append(JSON.toJSONString(easyUIDataGridJSON));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			pw.close();
		}

		
	}

	/**
	 * 删除一个用户
	 */
	public void deleteUser(HttpServletRequest request, HttpServletResponse response) {
		// 从页面获取id
		String id = request.getParameter("userId");
		Integer role_id = null;
		try {
			role_id = Integer.parseInt(id);
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}

		AdminUserService adminUserService = new AdminUserServiceImpl();

		// 判断删除是否成功
		boolean flag = adminUserService.deleteUser(role_id);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("删除失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		response.setContentType("application/json");
		try {
			response.getWriter().append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
	

